﻿// ===============================================================================
// Project Name        :    Gfw.BLL.Basic.Facilities
// Project Description :    
// ===============================================================================
// Class Name          :    CompanyDynamicAward
// Namespace:          :    Gfw.BLL.Basic.Facilities
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/6/25 20:54:23
// Update Time         :    2012/6/25 20:54:23
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.BLL.Basic.Account;
using Gfw.Repositories.Account;
using Gfw.Repository;

namespace Gfw.BLL.Basic.Facilities
{
    /// <summary>
    /// 公司帐户发放的用户动态奖金
    /// </summary>
    public class CompanyDynamicAward : IFacility
    {
        private string _userId;
        private double _award;
        private UserTradeTypeEnum _userType;
        private CompanyTradeTypeEnum _companyType;
     

        public CompanyDynamicAward(string userId, double award, UserTradeTypeEnum userType,
                                   CompanyTradeTypeEnum companyType)
        {
         
            _companyType = companyType;
            _userType = userType;
            _award = award;
            _userId = userId;
        }

        public void Execute(Repository.UnitOfWork unitOfWork = null)
        {
            UnitOfWork work = unitOfWork ?? new UnitOfWork();

            string awardSerialNo = Guid.NewGuid().ToString();

            //公司帐户取出amount金额
            ActCompanyBargainRepository companyBargain = new ActCompanyBargainRepository();
            companyBargain.AddOutlay(_userId, (int)_companyType, _award, "", awardSerialNo,
                                     work);



            //用户上税
            AwardTax at = new AwardTax(_userId, _award, awardSerialNo, null);
            at.Execute(work);

            //奖金帐户分配
            AwardAllot aa = new AwardAllot(_userId, at.IndividualAward(), awardSerialNo, null,
                                           UserTradeTypeEnum.Recommend);
            aa.Execute(work);

            //个人用户的奖金
            double individual = aa.IndividualAward();
            double tax = _award - individual;
            //插入动态奖金分配表
            ActUserDynamicawardRepository userAwardRepository = new ActUserDynamicawardRepository();
            userAwardRepository.AddAward(_userId, (int) _userType, _award, individual, tax,
                                         awardSerialNo, work);
            if (unitOfWork == null)
                work.Commit();
        }
    }
}
